package com.xcolud.redisdemo.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

/**
 * @author songshiming
 * @date 2023/3/7 下午11:14
 * @des  redis 实现计数器相关功能
 */
@Service
public class ConterServices {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 计数器 id 加  1
     * @param id
     * @return
     */
    public Long incr(String id) {
        return incr(id,1L);
    }

    /**
     * id 加 data 步长
     * @param id
     * @param data
     */
    public Long incr(String id ,Long data) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        return operations.increment(id, data);


    }

    /**
     * 查询 id 计数器的值
     * @param id
     * @return
     */
    public String sincr(String id) {

        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String s =operations.get(id);
        System.out.println(s);
        return  s ;
    }

}
